//
//  ActivityInfoViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/4.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "ActivityInfoViewController.h"
#import "ActivityInfoModel.h"
#import "ActivityInfoHeaderCell.h"
#import "ActivityInfoDetailCell.h"
#import "ActivityInfoTeacherCell.h"
#import "ActivityInfoDescriptionCell.h"
#import "UserInfoModel.h"
#import "OrderActivityButton.h"

@interface ActivityInfoViewController ()<UITableViewDataSource,UITableViewDelegate>

// 视图
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)OrderActivityButton *orderBtn;

// 数据
@property (nonatomic, retain)ActivityInfoModel *infoModel;

@end

@implementation ActivityInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"活动详情";
    self.view.backgroundColor = UIColor.whiteColor;

    // 设置视图
    [self setupView];

    // 刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self updateView];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 自定义方法
-(void)setupView {
    // 初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];

    // 预约活动
    self.orderBtn = [[OrderActivityButton alloc] initWithFrame:CGRectMake(0, self.tableView.bottomY, SCREEN_WIDTH, 40)];
    
    [self.view addSubview:self.orderBtn];
}

// 用code获取button状态
-(OrderActivityButtonType)buttonTypeWithCode:(NSInteger)code {
    switch (code){
        case 1:     // 可取消
            return CancelActivityEnable;
        case 3:     // 不可取消
            return CancelActivityDisable;
        case 2:     // 可预约
            return OrderActivityEnable;
        case 4:     // 不可预约
            return OrderActivityDisable;
        case 5:     // 活动进行中
        case 6:     // 活动进行中
        case 9:     // 活动进行中
            return DoingActivityDisable;
        case 8:     // 已评价
            return CheckEvaluateEnable;
        case 7:      // 未评价
            return CheckEvaluateDisable;
        case 10:      // 未参加此次活动，活动已结束
            return ActivityFinishedDisable;
    }
    return 0;
}


#pragma mark - 网络获取数据
-(void)updateView {

    // 获取Cell数据
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/TgetActivityInfo"
                                        parameters:@{@"aid": @(self.activityID)}
                                           success:^(NSDictionary *respondDictionary) {
                                               self.infoModel = [[ActivityInfoModel alloc] initWithDictionary:respondDictionary];
                                               [self.tableView reloadData];
                                               [self.tableView.mj_header endRefreshing];

                                               // 判断是否已经是预约过的活动
                                               [self.orderBtn setTitle:self.infoModel.status forState:UIControlStateNormal];

                                           } failure:^(NSError *error) {
                NSLog(@"%@",error);
                [SVProgressHUD showfailed];
                [self.tableView.mj_header endRefreshing];
            }];

}


#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityInfoBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell == nil) {
        if(indexPath.row == 0) {
            // 头部cell
            cell = [[ActivityInfoHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
        }if (indexPath.row == 1) {
            // 活动开始结束时间
            cell = [[ActivityInfoDetailCell alloc] initWithCellType:ActivityInfoDetailCellStartTimeType];
        }if (indexPath.row == 2) {
            // 活动地点
            cell = [[ActivityInfoDetailCell alloc] initWithCellType:ActivityInfoDetailCellLocationType];
        }if(indexPath.row == 3) {
            // 活动已报名人数
            cell = [[ActivityInfoDetailCell alloc] initWithCellType:ActivityInfoDetailCellOrderedNumberType];
        }if(indexPath.row == 4) {
            // 活动已可获得学分
            cell = [[ActivityInfoDetailCell alloc] initWithCellType:ActivityInfoDetailCellCanGetCreditType];
        }if(indexPath.row == 5) {
            // 导师介绍
            cell = [[ActivityInfoTeacherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
        }if (indexPath.row == 6) {
            cell = [[ActivityInfoDescriptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
        }
    }

    // 设置数据
    cell.model = self.infoModel;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {
        // 顶部显示
        return [tableView cellHeightForIndexPath:indexPath
                                    model:self.infoModel
                                  keyPath:@"model"
                                cellClass:[ActivityInfoHeaderCell class]
                         contentViewWidth:SCREEN_WIDTH];

    } else if (indexPath.row >= 1 && indexPath.row <= 4) {
        // 活动详情
        return 40;
    } else if (indexPath.row == 5) {
        // 导师介绍
        return [tableView cellHeightForIndexPath:indexPath
                                           model:self.infoModel
                                         keyPath:@"model"
                                       cellClass:[ActivityInfoTeacherCell class]
                                contentViewWidth:SCREEN_WIDTH];
    } else if(indexPath.row == 6) {
        // 活动简介
        return [tableView cellHeightForIndexPath:indexPath
                                           model:self.infoModel
                                         keyPath:@"model"
                                       cellClass:[ActivityInfoDescriptionCell class]
                                contentViewWidth:SCREEN_WIDTH];
    }
    return 0;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
}




@end
